package com.aq.appserver.server.file.controller;

import java.io.File;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.aq.appserver.common.bean.BaseResp;
import com.aq.appserver.common.util.ImageUtil;
import com.aq.appserver.server.account.service.AccountService;

@Controller
@RequestMapping("/file")
public class FileController {
	
	@Autowired
	private AccountService accountService;

	public static final String prix = System.currentTimeMillis() + "";
	
	private static Long index = 0l;
	
	private static Long sequence = 0l;
	
	public static synchronized Long getIndex()
	{
		return index ++;	
	}
	
	public static synchronized Long getSquence()
	{
		return sequence ++;	
	}
	
	@RequestMapping("/uploadTmpFile")
	@ResponseBody
	public Object uploadTmpFile(@RequestParam(value = "file", required = false) MultipartFile file,
			HttpServletRequest request)
	{
		BaseResp response = new BaseResp();	
		
		//判断是否是语音
		File tmpdir = null;
		if ("amr".equals(file.getOriginalFilename().substring  
				(file.getOriginalFilename().lastIndexOf(".") + 1))) {
			tmpdir = new File(request.getRealPath("/") + "/static/voice");
		} else {
			tmpdir = new File(request.getRealPath("/") + "/static/tmpdir");

		}
		if(tmpdir.exists() == false)
		{
			tmpdir.mkdir();
		}
		File tmpPath = new File(tmpdir,prix + "/" + getIndex());
		if(!tmpPath.exists())
		{
			tmpPath.mkdirs();
		}
		 try {
			String srcPath = new String(prix + "_" + getIndex() + file.getOriginalFilename().substring  
					(file.getOriginalFilename().lastIndexOf(".")));
			File tmpFile = new File(tmpPath, srcPath);
			file.transferTo(tmpFile);
			String fileUrl = tmpFile.getAbsolutePath().replace(request.getRealPath("/"), "").replace("\\", "/");
			response.put("fileUrl", fileUrl);
		} catch (Exception e) {
			e.printStackTrace();
			response.setResp("9999", "文件上传失败");
		} 
		return response;
		
	}
	
	@RequestMapping("/uploadContentFile")
	@ResponseBody
	public Object uploadContentFile(@RequestParam(value = "file", required = false) MultipartFile file,
			HttpServletRequest request)
	{
		BaseResp response = new BaseResp();	
		File tmpdir = new File(request.getRealPath("/") + "/static/content");

		if(tmpdir.exists() == false)
		{
			tmpdir.mkdir();
		}
		File tmpPath = new File(tmpdir,prix + "/" + getIndex());
		if(!tmpPath.exists())
		{
			tmpPath.mkdirs();
		}
		 try {
			String srcPath = new String(prix + "_" + getIndex() + file.getOriginalFilename().substring  
					(file.getOriginalFilename().lastIndexOf(".")));
			File tmpFile = new File(tmpPath, srcPath);
			file.transferTo(tmpFile);
			String desPath = tmpPath + "/" +  new String(prix + "_" + getSquence() + file.getOriginalFilename().substring  
					(file.getOriginalFilename().lastIndexOf(".")));
			File desFile = ImageUtil.saveImageAsJpg(tmpPath + "/" + srcPath, desPath, 240);
			//String path = sendFileToFileServer(tmpFile);
			String fileUrl = tmpFile.getAbsolutePath().replace(request.getRealPath("/"), "").replace("\\", "/");
			String thumbUrl = desFile.getAbsolutePath().replace(request.getRealPath("/"), "").replace("\\", "/");
			response.put("fileUrl", fileUrl);
			response.put("thumbUrl", thumbUrl);

		} catch (Exception e) {
			e.printStackTrace();
			response.setResp("9999", "文件上传失败");
		} 
		return response;
		
	}
}
